package homework4;

import java.io.*;
import java.util.ArrayList;

/**
 * RelativeSize()
 * 
 * @data 2013.7.11
 * @declaration read data, and calculate the Relative Size
 * @author 121250125
 * 
 * Listing Contents:
 * @method: main(String[] args)
 * 			,inln(ArrayList<Double> data)
 * 			,fromln(Double data)
 *          ,calculateStdDev(ArrayList<Double> data, double mean)
 *          ,calculateMean(ArrayList<Double> data, double mean)
 *          ,readFile(String path)
 */

public class RelativeSize {
	static BufferedReader br = null;
	static ArrayList<Double> classloc = new ArrayList<Double>();
	static ArrayList<Double> numberOfMethod = new ArrayList<Double>();
	static ArrayList<Double> pages = new ArrayList<Double>();
	static ArrayList<Double> quotion = new ArrayList<Double>();
	static double average = 0;
	static double stantard = 0;
	private static int line;
	
	public static void main(String[] args) {
		classloc = readFile("ClassLoc.txt");
		numberOfMethod = readFile("NumberOfMethod.txt");
		pages = readFile("pages.txt");
		
		for(int i = 0;i<classloc.size();i++) {
			quotion.add(classloc.get(i) / numberOfMethod.get(i));
		}
	
		inln(quotion);
		
		average = calculateMean(quotion ,0);
		stantard = calculateStdDev(quotion, average);
		
		System.out.println(String.format("VS = %.4f" , fromln(average - 2*stantard)));
		System.out.println(String.format("S = %.4f" , fromln(average - stantard)));
		System.out.println(String.format("M = %.4f" , fromln(average)));
		System.out.println(String.format("L = %.4f" , fromln(average + stantard)));
		System.out.println(String.format("VL = %.4f" , fromln(average + 2*stantard)));
	}// end
	

	/**
	 * inln(ArrayList<Double> data)
	 * 
	 * @declaration: Let data to ln(x)
	 * @param data
	 */
	public static void inln(ArrayList<Double> data) {
		for(int i = 0;i<data.size();i++) {
			data.set(i, Math.log(data.get(i)));
		}
	}// end
	

	/**
	 * fromln(Double data)
	 * 
	 * @declaration: Let ln(x) to e^x
	 * @param data
	 */
	public static Double fromln(Double data) {
		double e = Math.E;
		data = Math.pow(e, data);
		return data;
	}// end
	
	/**
	 * calculateStdDev(ArrayList<Double> data, double mean)
	 * 
	 * @declaration: Calculate the Standard deviation
	 * @param data,mean
	 * @return Standard deviation
	 */
	private static double calculateStdDev(ArrayList<Double> data, double mean) {
		double std = 0.0;
		for (double i : data) {
			std += (i - mean) * (i - mean);
		}
		std /= (data.size()-1);
		return Math.sqrt(std);
	}// end
	
	/**
	 * calculateMean(ArrayList<Double> data, double mean)
	 * 
	 * @declaration: Calculate the mean
	 * @param data,mean
	 * @return mean
	 */
	private static double calculateMean(ArrayList<Double> data, double mean) {
		for (double i : data) {
			mean += i;
		}
		return mean / data.size();
	}// end


	public static ArrayList<Double> readFile(String path) {
		ArrayList<Double> data = new ArrayList<Double>();
		String temp = "";
		try {
			br = new BufferedReader(new FileReader(path));
			while ((temp = br.readLine()) != null) {
				data.add(Double.parseDouble(temp));
				line++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}// end

}

